package com.demo2do.boyle.common.entity.customer.social;

import java.io.IOException;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.alibaba.fastjson.annotation.JSONField;
import com.demo2do.boyle.common.entity.AuditStatus;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.handler.impl.XinxiSmsHandlerImpl;

/**
 * 
 * @author Wilson
 */
@Entity
@Table(name = "social_account")
public class SocialAccount {

    @Id
    @GeneratedValue
    private Integer id;
    
    /** 账号 */
    private String code;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JSONField(serialize = false)
    private Customer customer;

    /** 用户名 */
    private String name;

    /** 别名 */
    private String nickName;

    /** 好友数 */
    private int fans;
    
    /** 预估价格  */
    private int variable;

    /** 预估价格 */
    private Integer estimatePrice;

    /** 社交账户类型 */
    @Enumerated(EnumType.ORDINAL)
    private SocialType type;

    /** 相关链接 */
    private String link;
    
    /** 上传认证图片时间 */
    private Date uploadTime;

    /** 认证图片 */
    private String picture;

    /** 账户注册时间 */
    private Date registerTime;

    /** 审核状态 */
    @Enumerated(EnumType.ORDINAL)
    private AuditStatus auditStatus;

    private Date createTime;

    @Column(name = "is_deleted")
    private boolean disabled;
    
    @Transient
    private boolean limited;

    /**
     * default constructor
     */
    public SocialAccount() {
    	
    }
    
    /**
     * Initialize socialAccount basic fields
     * 
     * @param code
     * @param picture
     * @param socialType
     */
    public SocialAccount(String code, String picture, SocialType socialType, Customer customer) {
    	this.customer = customer;
    	this.code = code;
    	this.picture = picture;
    	this.type = socialType;
    }
    
    /**
     * 
     * @return
     */
    public SocialAccount initialize() {
        this.createTime = new Date();
        this.disabled = false;
        this.auditStatus = AuditStatus.AUDITING;
        this.uploadTime = new Date();
        this.estimatePrice = 0;
        this.fans = 0;
        this.variable = 0;
        this.limited = true;
        return this;
    }


    /**
     * audit social account to audited
     * 
     * @return
     */
    public SocialAccount auditPass(int fans) {
    	this.fans = fans;
        this.auditStatus = AuditStatus.AUDITED;
        return this;
    }
    
    /**
     * audit social account to audited
     * 
     * @return
     */
    public SocialAccount auditNoPass(String url,String name,String password,String mobiles,String content,String sign) {
    	
    	this.auditStatus = AuditStatus.AUDIT_FAIL;
    	XinxiSmsHandlerImpl xinxiSmsHandler = new XinxiSmsHandlerImpl();
    	xinxiSmsHandler.setUrl(url);
    	xinxiSmsHandler.setName(name);
    	xinxiSmsHandler.setPassword(password);
    	xinxiSmsHandler.setMobiles(mobiles);
    	xinxiSmsHandler.setContent(content);
    	xinxiSmsHandler.setSign(sign);
    	try {
			xinxiSmsHandler.sendMessage();
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return this;
    }
    
    public SocialAccount changeFansAndNickName(int fans,String nickName) {
    	this.fans = fans;
    	this.nickName = nickName;
    	return this;
    }
    
    public SocialAccount reUpload(String picture) {
    	this.picture = picture;
    	this.uploadTime = new Date();
    	this.auditStatus = AuditStatus.AUDITING;
    	return this;
    }

    public String getSocialTypeLogo() {
        return type.getLogo();
    }

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @return the customer
	 */
	public Customer getCustomer() {
		return customer;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the nickName
	 */
	public String getNickName() {
		return nickName;
	}

	/**
	 * @return the fans
	 */
	public int getFans() {
		return fans;
	}
	
	/**
	 * @return the variable
	 */
	public int getVariable() {
		return variable;
	}

	/**
	 * @return the estimatePrice
	 */
	public Integer getEstimatePrice() {
		return estimatePrice;
	}

	/**
	 * @return the type
	 */
	public SocialType getType() {
		return type;
	}

	/**
	 * @return the link
	 */
	public String getLink() {
		return link;
	}

	/**
	 * @return the uploadTime
	 */
	public Date getUploadTime() {
		return uploadTime;
	}

	/**
	 * @return the picture
	 */
	public String getPicture() {
		return picture;
	}

	/**
	 * @return the registerTime
	 */
	public Date getRegisterTime() {
		return registerTime;
	}

	/**
	 * @return the auditStatus
	 */
	public AuditStatus getAuditStatus() {
		return auditStatus;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @return the disabled
	 */
	public boolean isDisabled() {
		return disabled;
	}

	/**
	 * @return the limited
	 */
	public boolean isLimited() {
		return limited;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @param customer the customer to set
	 */
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param nickName the nickName to set
	 */
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	/**
	 * @param fans the fans to set
	 */
	public void setFans(int fans) {
		this.fans = fans;
	}
	
	/**
	 * @param fans the variable to set
	 */
	public void setVariable(int variable) {
		this.variable = variable;
	}

	/**
	 * @param estimatePrice the estimatePrice to set
	 */
	public void setEstimatePrice(Integer estimatePrice) {
		this.estimatePrice = estimatePrice;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(SocialType type) {
		this.type = type;
	}

	/**
	 * @param link the link to set
	 */
	public void setLink(String link) {
		this.link = link;
	}

	/**
	 * @param uploadTime the uploadTime to set
	 */
	public void setUploadTime(Date uploadTime) {
		this.uploadTime = uploadTime;
	}

	/**
	 * @param picture the picture to set
	 */
	public void setPicture(String picture) {
		this.picture = picture;
	}

	/**
	 * @param registerTime the registerTime to set
	 */
	public void setRegisterTime(Date registerTime) {
		this.registerTime = registerTime;
	}

	/**
	 * @param auditStatus the auditStatus to set
	 */
	public void setAuditStatus(AuditStatus auditStatus) {
		this.auditStatus = auditStatus;
	}

	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * @param disabled the disabled to set
	 */
	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	/**
	 * @param limited the limited to set
	 */
	public void setLimited(boolean limited) {
		this.limited = limited;
	}
    
}
